package cn.xhjava.crm.workbench.service.impl;

import cn.xhjava.crm.commons.util.ExceptionUtil;
import cn.xhjava.crm.workbench.domain.Customer;
import cn.xhjava.crm.workbench.domain.DateDataMapVO;
import cn.xhjava.crm.workbench.domain.FunnelVO;
import cn.xhjava.crm.workbench.mapper.CustomerMapper;
import cn.xhjava.crm.workbench.service.CustomerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author Xiahu
 * @create 2023/7/18 0018
 */
@Service
public class CustomerServiceImpl implements CustomerService {
    private static final Logger log = LoggerFactory.getLogger(CustomerServiceImpl.class);
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public List<Customer> selectForPage(Map<String, Object> map) throws Exception {
        return customerMapper.queryCustomerByConditionForPage(map);
    }

    @Override
    public int selectCountForPage(Map<String, Object> map) throws Exception {
        return customerMapper.queryCountOfCustomerByCondition(map);
    }

    @Override
    public int delete(String[] ids) throws Exception {
        int res = 1;
        for (String id : ids) {
            customerMapper.deleteByPrimaryKey(id);
        }
        return res;
    }

    @Override
    public Customer selectById(String id) throws Exception {
        return customerMapper.selectByPrimaryKey(id);
    }

    @Override
    public int add(Customer customer) throws Exception {
        return customerMapper.insert(customer);
    }

    @Override
    public Customer selectDetailById(String id) throws Exception {
        return customerMapper.selectDetailById(id);
    }

    @Override
    public int update(Customer customer) throws Exception {
        return customerMapper.updateByPrimaryKeySelective(customer);
    }

    @Override
    public List<String> queryCustomerNamesLikeName(String customerName) throws Exception {
        return customerMapper.queryCustomerNamesLikeName(customerName);
    }

    @Override
    public DateDataMapVO queryCustomerCountGroupByCreateTime() throws Exception {
        List<FunnelVO> funnelVOList = customerMapper.queryCustomerCountGroupByCreateTime();
        DateDataMapVO res = new DateDataMapVO();
        LinkedList<String> date = new LinkedList();
        LinkedList<String> data = new LinkedList();
        for (FunnelVO vo : funnelVOList) {
            date.addLast(vo.getName());
            data.addLast(String.valueOf(vo.getValue()));
        }
        res.setDate(date);
        res.setData(data);
        return res;
    }
}
